<?php 

/**
 * 用户控制器U
 */

namespace Home\Controller;
use Think\Controller;
	class UController extends CommonController{
		//用户中心展示
		public function index()
		{
			$res = D('user')->getOneuser();
			// var_dump($res['id']);
			$sql = "select * from question where uid={$res['id']}";
			$uq = M('question')->query($sql);
			$sql = "select count(*) from question where uid={$res['id']}";

			$count = M('question')->query($sql);
			$sql = "select t1.* from question t1 inner join store t2 on t1.id=t2.qid where t2.uid={$_SESSION['id']}";
			$questions = M()->query($sql);
			// var_dump(count($questions));
			$this->assign('questions',$questions);
			// var_dump($res['e_status']);
			$count=current($count);
			// var_dump($count);
			$this->assign('uq',$uq);
			$this->assign('e_status',$res['e_status']);

			$this->assign('count',$count);

			layout('left');
			$this->display();
		}

		//基本设置展示
		public function edit()
		{
			$res = D('user')->getOneuser();
			$this->assign('res',$res);
			layout('left');
			$this->display();
			
			
		}

			//更新基本设置数据数据
			public function checkdate()
			{
				$data = I('post.');
				// var_dump($data);
				$user = D('user')->getOneuser();
				$sql = "select * from user where id!={$user['id']} && (email='{$data['email']}' || nickname='{$data['nickname']}')";
				$res = M('user')->query($sql);
				
				if ($res) {
					$arr = array(
					'error'=>0,
					'info'=>'邮箱或者用户名已经存在'
				);
				echo json_encode($arr);
				}else{
						if ($data['sex']==0) {
						$data['sex'] = "男";
					}else{
						$data['sex'] = '女';
					}
					$data['id'] = $_SESSION['id'];
					// var_dump($data);
					M('user')->save($data);
					$arr = array(
						'error'=>1,
						'info'=>'修改成功'
					);
					echo json_encode($arr);
				}
				
				
			}
			//基本设置修改密码 修改密码
			public function changePass()
			{
				
				$data['id'] = $_SESSION['id'];
				$password=I('post.password');
				$repassword = I('post.repassword');
				if ($password!=$repassword) {
					$arr = array(
					'error'=>0,
					'info'=>'前后密码不一致'
				);
				echo json_encode($arr);
				exit();
				}
				$data['password'] = md5($password);
				// var_dump($data);

					M('user')->save($data);
					$arr = array(
					'error'=>1,
					'info'=>'修改成功'
					);
				echo json_encode($arr);
				
			}

			//修改用户图像
			public function upload()
			{
			    $upload = new \Think\Upload();// 实例化上传类
			    $upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
			    $upload->savePath  =     ''; // 设置附件上传（子）目录
			    // 上传文件 
			    $info   =   $upload->upload();
			    // var_dump($info);
			    if(!$info) {// 上传错误提示错误信息
			        $this->error($upload->getError());
			    }else{// 上传成功
			        // $this->success('上传成功！');
			        $path =current($info);
			       $path = "Uploads/".$path['savepath'].$path['savename'];
			       
			       $res = M('user')->where(array('id'=>$_SESSION['id']))->save(array('face'=>$path));
			       unlink($_SESSION['face']);
			       if ($res) {
			       	 $_SESSION['face']=$path;
			       	 echo $path;
			       }else{
			       		echo "失败";
			       }
			    }
			}




			//我的消息展示
		public function msg()
		{
			layout('left');
			$this->display();
		}
		


		//产品授权展示
		public function panel()
		{
			layout('left');
			$this->display();
		}



		//我的主页展示
		public function main()
		{
			// var_dump($_SESSION['id']);
			// 取用户信息根据ID
			$id = I('get.id');
			$user = M('user')->find($id);
			$this->assign('user',$user);
			// var_dump($user);
			//取问题信息
			// $data['uid']=$id
			$question = M('question')->where(array('uid'=>$id))->select();
			// var_dump($question);
			$this->assign('question',$question);
			//取回答信息
			$sql = "select t1.title,t2.* from question t1 inner join answer t2 on t1.id=t2.qid where t1.uid={$id}";
			$res = M()->query($sql);
			// var_dump($res);
			// $answer = M('answer')->where(array('uid'=>$id))->select();
			$this->assign('res',$res);

			$this->display();
		}

		public function store()
		{
			layout('left');
			$this->display();
		}
		public function find_nick()
		{
			$nickname=I('get.nickname');
			// var_dump($nickname);
			$id = M('user')->where(array('nickname'=>$nickname))->field('id')->find();
			$id= current($id);
			$url =U('home/u/main',array('id'=>$id));
			header('location:'.$url);
		}





	}

















 ?>